import {wxRequest} from '../../utils/server.js'; 
const _wxRequest = new wxRequest();
const app = getApp()
Page({   
  data: {
    imgUrl: app.globalData.imgUrl,
    odrList: [{
      id: 1,
      icon: '/icon/daifukuan.png',
      name: '待付款',
      count: 'payCount'
    },
    {
      id: 2,
      icon: '/icon/daishouhuo.png',
      name: '待收货',
      count: 'deliveryCount'
    }],
    userInfo: wx.getStorageSync('userInfo') || {},
    uploadUrl: wx.getStorageSync('uploadUrl'),
    goodsList: [],
    myPresentOdr:[],
    type:'',
    preheatPresentList:[]
  },
 

  onShow:function(){
    this.getUserInfo();  
    if (wx.getStorageSync('userInfo')){
      this.getGoodsList();
    }
    this.myPresent();
    this.findPreheatPresent()
  },


  //跳转到订单详情
  toOrder:function(e){
    var status = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order/index?status=' + status,
    })
  },

  //去设置
  toSetting: function () {
    wx.navigateTo({
      url: '/pages/menber/setting/index',
    })
  },

  //商品关注
  arrGds:function(){
    wx.navigateTo({
      url: '/pages/attention/goods',
    })
  },

  arrStore: function () {
    wx.navigateTo({
      url: '/pages/attention/store',
    })
  },
  
  //获取用户信息
  getUserInfo: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/customer/getLoginCustomer',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      //非微信头像拼接头部
      if (res.vo && res.vo.avatar){
        var avatar = res.vo.avatar
        if (avatar.indexOf('https')==-1){ 
          res.vo.avatar = this.data.uploadUrl + avatar
        } 
      }

      this.setData({
        userInfo: res.vo
      })

      wx.setStorageSync('userInfo', res.vo);
    })
  },

  //商品列表
  getGoodsList: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/gds/goods/recommend',
      method: 'POST',
      data: {
        cusId: wx.getStorageSync('userInfo').id,
        pageNo: 1,
        pageSize: 8
      }
    }, (res) => {
      let goodsList = res.list;
      this.setData({
        goodsList 
      });
    })
  },
  
  toOrderList:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order/index?status='+id,
    })
  },

  //评价
  toAssess:function(){
    wx.navigateTo({
      url: '/pages/assess/list',
    })
  }, 

  toAftersale:function(){
    var url = '/pages/aftersale/index'
    if (this.data.userInfo.refundCount > 0){
      url = '/pages/aftersale/index?serviceType=2'
    }
    wx.navigateTo({
      url:url,
    })
  },

  //获取首礼单信息
  myPresent: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/shoppingCart/myPresent',
      method: 'POST',
    }, (res) => { 
      this.setData({
        myPresentOdr:res.list
      })
 
    })
  },

  checkMyPresentOdr:function(){
    if (this.data.myPresentOdr.length==0){
      wx.showToast({
        icon: 'none',
        title: '将商品分享给朋友，TA首次下单后您将免费获得该商品',
        duration: 5000
      })
    }else{
      var myPresentOdr=this.data.myPresentOdr;
      var totalPrice=0
      myPresentOdr.map(item=>{
        totalPrice += item.qty * item.price
      })
        wx.setStorageSync('goodsOdrList', this.data.myPresentOdr)
        wx.setStorageSync('odrTotalPrice', totalPrice)
        wx.navigateTo({
          url: '/pages/settle/index?type=1',
        }) 
    }
  },


  //获取预热中信息
  findPreheatPresent: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/findPreheatPresent',
      method: 'POST',
    }, (res) => {
      console.log(res,'获取预热中信息') 
      this.setData({
        preheatPresentList:res.list
      })
    })
  },

  //预热中跳转
  checkpreheatPresentList:function(){
    if (this.data.preheatPresentList.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '将商品分享给朋友，TA首次下单后您将免费获得该商品',
        duration: 5000
      })
    } else { 
      wx.navigateTo({
        url: '/pages/preheatPresen/list',
      })
    }
  }

})